/*              Copyright(c) 2008 Dave Sexton              *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Drawing;

namespace DaveSexton.DocToMaml
{
	public enum StartupProject
	{
		NewProject,
		OpenLastProject, 
		OpenProject
	}

	internal sealed class ProgramSettings
	{
		#region Public Properties
		public static readonly string DefaultProjectDirectory = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), @"DocToMaml\Projects\");
		public static readonly string DefaultHelpFileName = Application.ProductName + " Help.chm";
		public static readonly string DefaultHelpFile = System.IO.Path.Combine(Application.StartupPath, DefaultHelpFileName);

		public StartupProject Startup
		{
			get
			{
				return settings.Startup;
			}
			set
			{
				settings.Startup = value;
			}
		}

		public string ProjectDirectory
		{
			get
			{
				return (string.IsNullOrEmpty(settings.ProjectDirectory)) ? DefaultProjectDirectory : settings.ProjectDirectory;
			}
			set
			{
				settings.ProjectDirectory = 
					(string.Equals(value, DefaultProjectDirectory, StringComparison.OrdinalIgnoreCase))
					? null : value;
			}
		}

		public string LastProject
		{
			get
			{
				return settings.LastProject;
			}
			set
			{
				settings.LastProject = value;
			}
		}

		public bool MinimizeToTray
		{
			get
			{
				return settings.MinimizeToTray;
			}
			set
			{
				settings.MinimizeToTray = value;
			}
		}

		public bool MinimizeOnClose
		{
			get
			{
				return settings.MinimizeOnClose;
			}
			set
			{
				settings.MinimizeOnClose = value;
			}
		}

		public string HelpFile
		{
			get
			{
				return helpFile ?? DefaultHelpFile;
			}
			set
			{
				helpFile = value;
			}
		}
		#endregion

		#region Private / Protected
		private readonly ProgramForm form;
		private readonly Properties.Settings settings;
		private bool stateChanged;
		private Point previousLocation;
		private Size previousSize;
		private string helpFile;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ProgramSettings" /> class.
		/// </summary>
		public ProgramSettings(ProgramForm form)
			: this(form, Properties.Settings.Default)
		{
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="ProgramSettings" /> class.
		/// </summary>
		public ProgramSettings(ProgramForm form, Properties.Settings settings)
		{
			if (form == null)
				throw new ArgumentNullException("form");

			if (settings == null)
				throw new ArgumentNullException("settings");

			this.form = form;
			this.settings = settings;

			form.Resize += Form_Resize;
			form.LocationChanged += Form_LocationChanged;
		}
		#endregion

		#region Methods
		public void Load()
		{
			if (!string.IsNullOrEmpty(settings.HelpFile))
				helpFile = settings.HelpFile;

			Point? location = settings.Location;

			if (location.HasValue)
			{
				form.StartPosition = FormStartPosition.Manual;
				form.Location = location.Value;
			}

			Size? size = settings.Size;

			if (size.HasValue)
				form.Size = size.Value;

			// Always set the window state after setting the location to support multiple monitors.
			// If the form will be maximized then setting the location first will ensure that it's expanded on the correct screen.
			form.WindowState = settings.FormWindowState;

			ToolStripManager.LoadSettings(form);
		}

		public void Save()
		{
			settings.FormWindowState = (form.WindowState == FormWindowState.Minimized) ? FormWindowState.Normal : form.WindowState;

			if (settings.FormWindowState == FormWindowState.Normal)
			{
				settings.Location = form.Location;
				settings.Size = form.Size;
			}
			else if (stateChanged)
			{
				settings.Location = previousLocation;
				settings.Size = previousSize;
			}

			if (!string.IsNullOrEmpty(helpFile))
				settings.HelpFile = helpFile;

			settings.Save();

			ToolStripManager.SaveSettings(form);
		}
		#endregion

		#region Event Handlers
		private void Form_Resize(object sender, EventArgs e)
		{
			if (form.WindowState == FormWindowState.Normal)
				previousSize = form.Size;

			stateChanged = stateChanged | form.WindowState != FormWindowState.Normal;
		}

		private void Form_LocationChanged(object sender, EventArgs e)
		{
			if (form.WindowState == FormWindowState.Normal)
				previousLocation = form.Location;
		}
		#endregion
	}
}
